package com.neusoft.yuejian12.yuejian_12.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.javabean.AttentionBean;
import com.neusoft.yuejian12.yuejian_12.javabean.IsAttentionBean;
import com.neusoft.yuejian12.yuejian_12.model.ClubModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.CLUB_PHOTO_URL;

public class ClubDetailActivity extends AppCompatActivity {
    private ImageView left;
    private ImageView collect;
    private ImageView c_pic;
    private TextView c_name;
    private TextView c_address;
    private TextView c_time;
    private TextView c_info;
    private TextView c_phone;
    private int c_id;
    private String  flag;
    private int u_id;
    private String  coresult="";
    private Button activity_club,course_club,video_club;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id",0);
        initView();
        getClubData();
        initEvent();
        isBeCollected();
    }


    private void initView() {

        collect = (ImageView) findViewById(R.id.collect);
        left = (ImageView) findViewById(R.id.left);
        c_name = (TextView) findViewById(R.id.c_name);
        c_address = (TextView) findViewById(R.id.c_address);
        c_time = (TextView) findViewById(R.id.c_time);
        c_info = (TextView) findViewById(R.id.c_info);
        c_phone = (TextView) findViewById(R.id.c_phone);
        c_pic = (ImageView) findViewById(R.id.c_pic);

        activity_club = (Button) findViewById(R.id.activity_club);
        course_club = (Button) findViewById(R.id.course_club);
        video_club = (Button) findViewById(R.id.video_club);

    }

    private void initEvent(){
//        final Bundle bundle = this.getIntent().getExtras();
//       final int c_id=bundle.getInt("c_id");

        c_id=getIntent().getIntExtra("c_id",-1);

        left.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        activity_club.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ClubDetailActivity.this,ClubActActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("c_id", c_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


        course_club.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ClubDetailActivity.this,ClubCourseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("c_id", c_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        video_club.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ClubDetailActivity.this,ClubVideoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("c_id", c_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectClub();
            }
        });

 }
    private void getClubData() {
         Bundle bundle = this.getIntent().getExtras();
         //int cid=bundle.getInt("c_id");
    //    System.out.print(cid);
        String cname = bundle.getString("c_name");
        String caddress = bundle.getString("c_address");
        String cphone = bundle.getString("c_phone");
        String cinfo = bundle.getString("c_info");
        // int tprice = bundle.getInt("t_price");
        String cpic = CLUB_PHOTO_URL + bundle.getString("c_pic");
        String ctime = bundle.getString("c_time");
        c_name.setText(cname);
        c_address.setText(caddress);
        c_phone.setText(cphone);
        c_info.setText(cinfo);
        c_time.setText(ctime);
        Picasso.with(this).load(cpic).into(c_pic);

    }
    private void isBeCollected(){
        //判断是否关注
        ClubModel clubModel=new ClubModel();
        Call<IsAttentionBean> callIsCollect=clubModel.getIsCollected(u_id,c_id,"4");
        callIsCollect.enqueue(new Callback<IsAttentionBean>() {
            @Override
            public void onResponse(Call<IsAttentionBean> call, Response<IsAttentionBean> response) {
                String result=response.body().getCollected();
                if ("0".equals(result)) {

                    collect.setImageResource(R.drawable.ic_collect_hui);
                    coresult = "no";
                } else {
                    coresult = "yes";
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
            }

            @Override
            public void onFailure(Call<IsAttentionBean> call, Throwable t) {

            }
        });

    }

    //关注俱乐部
    private void collectClub(){
        ClubModel clubModel=new ClubModel();
        Call<AttentionBean> callCollectClub=clubModel.userCollectClub(u_id, c_id);
        callCollectClub.enqueue(new Callback<AttentionBean>() {
            @Override
            public void onResponse(Call<AttentionBean> call, Response<AttentionBean> response) {

                if (response.body().getSuccess().equals("1")) {
                    if ("yes".equals(coresult)) {
                        Toast.makeText(ClubDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                        collect.setImageResource(R.drawable.ic_collect_hui);
                    }
                    else{
                        Toast.makeText(ClubDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        collect.setImageResource(R.drawable.ic_collect_red);
                    }
                    refresh();
                }

            }

            @Override
            public void onFailure(Call<AttentionBean> call, Throwable t) {

            }


        });
    }
    private void refresh(){
        isBeCollected();
    }

}